package com.adee.gulimall.product.service.impl;

import com.adee.gulimall.common.utils.PageUtils;
import com.adee.gulimall.common.utils.Query;
import com.adee.gulimall.product.dao.BrandDao;
import com.adee.gulimall.product.dao.CategoryBrandRelationDao;
import com.adee.gulimall.product.entity.BrandEntity;
import com.adee.gulimall.product.entity.CategoryBrandRelationEntity;
import com.adee.gulimall.product.service.CategoryBrandRelationService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
    @Autowired
    CategoryBrandRelationDao relationDao;

    @Autowired
    BrandDao brandDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCatId(Map<String, Object> params) {
        Long catId = Long.parseLong((String)params.get("catId"));
        // 分页查询品牌-分类关联表
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        List<CategoryBrandRelationEntity> relas = page.getRecords();
        List<Long> brandIds = relas.stream().map(rela -> rela.getBrandId()).collect(Collectors.toList());
        // 根据品牌id列表查询品牌信息
        List<BrandEntity> brandList = brandDao.selectBatchIds(brandIds);
        return new PageUtils(brandList, (int)page.getTotal(), (int)page.getSize(), (int)page.getCurrent());
    }

    @Override
    public List<BrandEntity> queryListByCatId(Long catId) {
        List<CategoryBrandRelationEntity> relas =
                relationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>()
                        .eq("catelog_id", catId));
        List<Long> brandIds = relas.stream().map(rela -> rela.getBrandId()).collect(Collectors.toList());
        List<BrandEntity> brandList = brandDao.selectBatchIds(brandIds);
        return brandList;
    }

    @Override
    public void updateBrand(Long brandId, String name) {
        CategoryBrandRelationEntity r = new CategoryBrandRelationEntity();
        r.setBrandName(name);
        this.update(r, new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
    }

    // 使用mybatisx插件
    @Override
    public void updateCategory(Long catId, String name) {
//        CategoryBrandRelationEntity r = new CategoryBrandRelationEntity();
//        r.setCatelogName(name);
//        this.update(r, new UpdateWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        baseMapper.updateCategory(catId, name);
    }


}